<?php


namespace app\index\controller;


use app\common\controller\UserController;
use app\common\model\EmployPurchase;
use app\common\model\EmployPurchaseOrder;
use app\common\model\Model;
use app\common\model\ServerGood;
use app\common\model\ServerOrder;

class Order extends UserController
{
    /**
     * 我的服务订单
     */
    function my_server_order(){
        $uid = $this->getUid();
        $data = ServerOrder::simpleList([
            "query"=>function(Model $query) use($uid) {
                $query->where(["uid"=>$uid]);
            },
        ]);
        $this->result_success($data);
    }

    /**
     * 我的采购订单
     */
    function my_purchase_order(){
        $uid = $this->getUid();
        $data = EmployPurchaseOrder::simpleList([
            "query"=>function(Model $query) use($uid) {
                $query->where(["uid"=>$uid]);
            },
        ]);
        $this->result_success($data);
    }
    /*
     * 创建服务订单
     */
    function create_server_order(){
        $goods_id = input("goods_id");
        $good = ServerGood::getById($goods_id);
        if(empty($good)){
            $this->result_error("订单创建失败");
        }
        $fee = $good->fee;
        $param = input();
        $param = array_merge($param,$good);
        $param["order_no"]= ServerOrder::orderNO($goods_id);
        $param["server_goods_id"]= $goods_id;
        $param["fee"]= $fee;
        $data = ServerOrder::simpleAdd($param);
        $this->result_success($data,0,"订单创建成功");
    }

    /**
     * 创建采购订单
     */
    function create_purchase_order(){
        $purchase_id = input("purchase_id");
        $purchase = EmployPurchase::getById($purchase_id);
        if(empty($purchase)){
            $this->result_error("订单创建失败");
        }
        $fee = $purchase->fee;
        $param = input();
        $param = array_merge($param,$purchase);
        $param["order_no"]= EmployPurchaseOrder::orderNO($purchase_id);
        $param["employ_purchase_id"]= $purchase_id;
        $param["frozen_fee"]= $fee;
        $data = ServerOrder::simpleAdd($param);
        $this->result_success($data,0,"订单创建成功");
    }
}